<script setup lang="ts">
import web3 from 'web3'
import mButton from "@/components/m-button.vue"

const getBalance = async () => {
    try {
        const accounts = await window.ethereum.request({ method: 'eth_accounts' });
        const fromAddress = accounts[0];
        if (!fromAddress) {
            uni.showToast({ title: '请先连接钱包', icon: "none" });
            return;
        }
        const balance = await window.ethereum.request({ method: 'eth_getBalance', params: [fromAddress, 'latest'] });
        const balanceInEther = web3.utils.fromWei(balance, 'ether');
        uni.showToast({ title: `当前账户余额为：${balanceInEther} ETH`, icon: "none" });
        console.log(`Current balance of ${fromAddress}: ${balanceInEther} ETH`);
    } catch (error) {
        console.error(error);
    }
}
</script>
<template>
    <mButton @click="getBalance">获取账户余额</mButton>
</template>
